//
// Created by Administrator on 2021/8/12.
//
// 找最长的回文子序列
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        int n = (int) s.size();
        vector<vector<int>> dp(n, vector<int>(n, 0));
        for (int i = 0; i < n; ++i) {
            dp[i][i] = 1;
        }
        // 因为需要dp[i+1][j-1]的信息，所以i需要从大到小，j需要从小到大
        for (int i = n - 1; i >= 0; --i) {
            for (int j = i + 1; j < n; ++j) {
                if (s[i] == s[j]) {
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                } else {
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[0][n - 1];
    }
};

int main() {
    Solution solution;
    cout << solution.longestPalindromeSubseq("abcbad") << endl;
    return 0;
}
